package pdf;
/** 
* @author 杨迅
* @number 16301024
* @time 2018年4月24日 下午11:16:42
* FileName:PDFAndImage.java
* Description:
*/

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;  


public class PDFAndImage {
	///////////////////////////////////PDF转图片////////////////////////////////////////
	private String filepath = "";
	private String outputImagesLocation = "";
	public void setInputPDFPath(String filepath){
		this.filepath = filepath;
	}
	public void setOutputImagesLocation(String outputLocation){
		this.outputImagesLocation = outputLocation;
	}
	public void pdfToImage(String name){
		File file = new File(filepath);
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 296);
                //BufferedImage srcImage = resize(image, image.getWidth(), image.getHeight());
                ImageIO.write(image, "PNG", new File(outputImagesLocation + name + (i+1) + ".png"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
	///////////////////////////////////图片转PDF//////////////////////////////////////
	private String[] imagePath = new String[100];
	private List<BufferedImage> images=new ArrayList<BufferedImage>();
	private int numberOfImage = 0;
	private String pdfPath = "";
	public void addImage(String imagePath) throws FileNotFoundException, IOException{
		this.imagePath[numberOfImage] = imagePath;
		this.numberOfImage++;
		BufferedImage image = ImageIO.read(new FileInputStream(imagePath));
		this.images.add(image);
	}
	public void setOutputPDFPath(String pdfPath){
		this.pdfPath = pdfPath;
	}
	public void imageToPDF() throws DocumentException, MalformedURLException, IOException{
		// 输入流
        FileOutputStream fos = new FileOutputStream(pdfPath);
        // 创建文档
        Document doc = new Document(null, 0, 0, 0, 0);
        // 写入PDF文档
        PdfWriter.getInstance(doc, fos);
        for(int i = 0; i < numberOfImage; i++){
        	// 根据图片大小设置文档大小
            doc.setPageSize(new Rectangle(images.get(i).getWidth(), images.get(i).getHeight()));
            // 实例化图片
            Image img = Image.getInstance(imagePath[i]);
            // 添加图片到文档
            doc.open();
            doc.add(img);
        }
        doc.close();
	}
//	public static void main(String[] args) throws IOException { 
//		PDFAndImage p = new PDFAndImage();
//		p.setInputPDFPath("E:/many1.pdf");
//		p.setOutputImagesLocation("E:/");
//		p.pdfToImage();
//	} 
	
//	public static void main(String[] args) throws IOException, DocumentException { 
//        PDFAndImage p = new PDFAndImage();
//        p.addImage("E:/pdfImage1.png");
//        p.addImage("E:/pdfImage2.png");
//        p.addImage("E:/pdfImage3.png");
//        p.addImage("E:/pdfImage4.png");
//        p.setOutputPDFPath("E:/many5.pdf");
//        p.imageToPDF();
//	} 
}
